package com.xish.util;

import java.util.Map;
import java.util.Set;
import java.util.TreeMap;

/**
 * Created by zhanghuyi on 15/7/27.
 */
public class SignUtil {

    public static String getSign(TreeMap<String,String> treeMap,String secret){
        if(treeMap!=null&&!treeMap.isEmpty()){
            Set<Map.Entry<String, String>> entries = treeMap.entrySet();
            StringBuilder sb = new StringBuilder();
            for(Map.Entry<String, String> e:entries){
                sb.append(e.getKey()).append("=").append(e.getValue()).append("&");
            }
            sb.append(secret);
            String sign = MD5.calculate(sb.toString());
            return sign;
        }
        return "";
    }

    public static void main(String[] args) throws Exception{
        TreeMap<String,String> treeMap = new TreeMap<String,String>();
        treeMap.put("app_key","wanzhoumo");
        treeMap.put("id","26056");
        treeMap.put("user_id","1");
        treeMap.put("begin_date","2015-07-27");
        treeMap.put("end_date","2015-08-27");
        treeMap.put("type", "datelist");
        String sign = getSign(treeMap,"089dc39dd4b0d5b44f527eaa260ddd1d");
        treeMap.put("sign", sign);
        System.out.println(sign);

//        Set<Map.Entry<String, String>> entries = treeMap.entrySet();
//        JsonObject jsonObject = new JsonObject();
//        for(Map.Entry<String, String> e:entries){
//            jsonObject.addProperty(e.getKey(), e.getValue());
//        }
//        System.out.println(jsonObject.toString());
//        HttpClient client = new HttpClient();
//        //使用POST方法
//        PostMethod method = new PostMethod("http://trade5.wanzhoumo.com/api/" + "activity");
//        method.setRequestEntity(new StringRequestEntity(jsonObject.toString(),"application/x-www-form-urlencoded;charset=UTF-8","UTF-8"));
//
//        method.setRequestHeader("X-HTTP-Method-Override", "get");
//        client.executeMethod(method);
//        System.out.println(method.getStatusLine());
//        String resp = method.getResponseBodyAsString();
//        System.out.println(resp);
        String resp =  HttpClientUtil.post("http://trade5.wanzhoumo.com/api/" + "activity",treeMap);
        System.out.println(resp);
    }
}
